<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/20
 * Time: 14:09
 */
namespace app\api\controller;
use app\api\model\RoomOrdersModal;
use app\common\controller\BaseController;
use function PHPSTORM_META\elementType;
use think\Validate;
use app\index\model\RoomsModel;
class RoomOrders extends BaseController{
    //生成订单
    public function addOrder(){
        $custom_name = input('post.custom_name');
        $custom_id_card =input('post.custom_id_card');
        $custom_sex = input('post.custom_sex');
        if($custom_sex=="男"){
            $custom_sex=1;
        }else if($custom_sex=="女"){
            $custom_sex=2;
        }else{
            return $this->ajaxReturn(400,null,'性别不合法');
        }
        $custom_nation =input('post.custom_nation');
        $custom_residence = input('post.custom_residence');
        $custom_police_address =input('post.custom_police_address');
        $custom_valid_date = input('post.custom_valid_date');
        $custom_birth_date =input('post.custom_birth_date');
        $room_code =input('post.room_code');
        $retinue =input('post.retinue');
        $room_number =input('post.room_number');
        $rule = [
            'custom_name' => 'require',
            'custom_id_card' => 'require|length:18',
            'custom_sex' => 'require',
            'custom_nation' => 'require',
            'custom_residence' => 'require',
            'custom_police_address' => 'require',
            'custom_valid_date' => 'require',
            'custom_birth_date' => 'require',
            'room_code' => 'require',
            'room_number' => 'require',
        ];
        $data = [
            'custom_name' => $custom_name,
            'custom_id_card' => $custom_id_card,
            'custom_sex' => $custom_sex,
            'custom_nation' => $custom_nation,
            'custom_residence' => $custom_residence,
            'custom_police_address' => $custom_police_address,
            'custom_valid_date' => $custom_valid_date,
            'custom_birth_date' => $custom_birth_date,
            'room_code' => $room_code,
            'room_number' => $room_number,
        ];
        $msg = [
            'custom_name.require' => '客户姓名不能为空',
            'custom_id_card.require' => '身份证号不能为空',
            'custom_id_card.length' => '身份证号长度应为18位',
            'custom_sex.require' => '客户性别不能为空',
            'custom_nation.require' => '客户民族不能为空',
            'custom_residence.require' => '客户户籍地址不能为空',
            'custom_police_address.require' => '身份证签发机关不能为空',
            'custom_valid_date.require' => '身份证有效日期不能为空',
            'custom_birth_date.require' => '客户出生年月日不能为空',
            'room_code.require' => '房间编码不能为空',
            'room_number.require' => '房间数量不能为空',
        ];
        $validate = new Validate($rule,$msg);
        if (!$validate->check($data)) {
            return $this->ajaxReturn(400,null,$validate->getError());
        }
        $model = new RoomOrdersModal();
        $map =[
            'custom_id_card'=>$custom_id_card,
            'status'=>1
        ];
        $data = $model->where($map)->select();
        if($data){
            return $this->ajaxReturn(400,null,'客户信息重复');
        }
        $map1 =[
            'room_code'=>$room_code,
            'status'=>1
        ];
        $data1 = $model->where($map1)->group('room_code')->field('sum(room_number) as sum')->find();
        if($data1){
            $room_sum = $room_number+$data1->sum;
        }else{
            $room_sum = $room_number;
        }
        $model1 = new RoomsModel();
        $data2 = $model1->where('room_code',$room_code)->field('room_num')->find();
        if($data2->room_num<$room_sum){
            return $this->ajaxReturn(400,$room_sum,'房间数量超出');
        }

        $model->custom_name = $custom_name;
        $model->custom_id_card =$custom_id_card;
        $model->custom_sex = $custom_sex;
        $model->custom_nation =$custom_nation;
        $model->custom_residence = $custom_residence;
        $model->custom_police_address =$custom_police_address;
        $model->custom_valid_date = $custom_valid_date;
        $model->custom_birth_date =$custom_birth_date;
        $model->create_time = date("Y-m-d H:i:s");
        $model->room_code =$room_code;
        $model->check_in_date = date("Y-m-d H:i:s");
//        $model->check_out_date =$check_out_date;
//        $model->status = $status;
        $model->retinue =$retinue;
        $model->room_number = $room_number;
        $model->save();
        return $this->ajaxReturn(200,$model,"添加成功");
    }
    //结束订单
    public function updateOrder(){
        $order_id = input('post.order_id') ;
        //退订房间数
        $room_number = input('post.room_number',0);
        //退订人数.
        $retinue = input('post.retinue',0);
        if(empty($order_id)){
            return $this->ajaxReturn(400,null,"订单号不能为空");
        }
        $model = new RoomOrdersModal();
        $data1 = $model->where("order_id",$order_id)->find();
        if(!$data1){
            return $this->ajaxReturn(400,null,"订单号不存在");
        }
        if($data1->retinue-$retinue<=0){
            return $this->ajaxReturn(400,null,"退订人数不合法");
        }
        if($data1->room_number-$room_number<=0){
            return $this->ajaxReturn(400,null,"退订房间数不合法");
        }
        if($room_number==0&&$retinue==0){
            $data['check_out_date'] =date("Y-m-d H:i:s");
            $data['status'] =2;
        }else{
            $data['retinue'] = $data1->retinue-$retinue;
            $data['room_number'] = $data1->room_number-$room_number;
        }
        $type = $model->where("order_id",$order_id)->update($data);
        return $this->ajaxReturn(200,$type,"修改成功");
    }

    //查询订单
    public function queryOrder(){
        $page = input("get.page",1);
        $limit = input("get.limit",5);
        $account = input('get.account');
        $policeStation = input('get.policeStation');
        $map['d.account'] = $account;
        $model = new RoomOrdersModal();
        $data = $model->alias('a')
            ->join('think_rooms b','b.room_code = a.room_code','left')
            ->join('think_user_room c','b.id=c.room_id','left')
            ->join('think_app_users d','c.uid= d.id','left')
            ->join('think_police_station e','e.id=b.police_station_id','left')
            ->field('a.*,b.police_station_id,b.id,c.*,d.*,e.*')
            ->where($map)
            ->page($page,$limit)
            ->select();
        return $this->ajaxReturn(200,$data,"修改成功");
    }

    //条件获取数据库总数据条数
    public function getPageLimit(){
        $account = input('get.account');
        $map['d.account'] = $account;
        $model = new RoomOrdersModal();
        $data = $model->alias('a')
            ->join('think_rooms b','b.room_code = a.room_code','left')
            ->join('think_user_room c','b.id=c.room_id','left')
            ->join('think_app_users d','c.uid= d.id','left')
            ->join('think_police_station e','e.id=b.police_station_id','left')
            ->field('a.room_code,b.police_station_id,b.id,c.*,d.*,e.*')
            ->where($map)
            ->count();
        return $this->ajaxReturn(200,$data,'获取成功');
    }
}